package com.cofluent.web.client;

import com.cofluent.web.client.model.Build;
import com.cofluent.web.client.provider.DynaTableDataProvider;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;

/**
 * A data provider that bridges the provides row level updates from the data
 * available through a <@link SchoolCalendarService>.
 */
public class BuildInformationProvider implements DynaTableDataProvider
{
	private final BuildInformationServiceAsync calService;
//	private BuildFilter filter;
	protected Build[] lastBuilds;

//	private int lastMaxRows = -1;
//
//	private Person[] lastPeople;
//
//	private int lastStartRow = -1;

	public BuildInformationProvider()
	{
//		this(null);
//	}
//	
//	public BuildInformationProvider(BuildFilter filter)
//	{
//		this.filter=filter;
		// Initialize the service.
		//
		calService = (BuildInformationServiceAsync) GWT
				.create(BuildInformationService.class);

		// By default, we assume we'll make RPCs to a servlet, but see
		// updateRowData(). There is special support for canned RPC responses.
		// (Which is a totally demo hack, by the way :-)
		// 
		ServiceDefTarget target = (ServiceDefTarget) calService;

		// Use a module-relative URLs to ensure that this client code can find
		// its way home, even when the URL changes (as might happen when you
		// deploy this as a webapp under an external servlet container).
		String moduleRelativeURL = GWT.getModuleBaseURL() + "builds";
		target.setServiceEntryPoint(moduleRelativeURL);
	}

	public void updateRowData(//final int startRow, final int maxRows,
			final AsyncCallback asyncCallback) {
		// Check the simple cache first.
		//
		if(lastBuilds!=null)
			asyncCallback.onSuccess(lastBuilds);
//			pushResults(acceptor, lastBuilds);;
//		if (startRow == lastStartRow) {
//			if (maxRows == lastMaxRows) {
				// Use the cached batch.
				//
//				pushResults(acceptor, startRow, lastPeople);
//				return;
//			}
//		}

		// Decide where to get the data.
		// This is very unusual. Normally, you'd set the service entry point
		// only once. But since this demo runs without servlet support, we
		// play a game with URLs to fetch canned RPC responses from static files.
		// So, as you might guess, this is really fragile: changing the number
		// of visible rows in the table will cause different batch sizes, and
		// the URL naming trick below will *definitely* break.
		//
//		if (USE_STATIC_RPC_ANSWERS) {
//			ServiceDefTarget target = (ServiceDefTarget) calService;
//			String staticResponseURL = GWT.getModuleBaseURL();
//			staticResponseURL += "calendar" + startRow + ".txt";
//			target.setServiceEntryPoint(staticResponseURL);
//		}

		// Actually fetch the data remotely.
		//
		calService.getBuilds(new AsyncCallback() {
			public void onFailure(Throwable t) {
				asyncCallback.onFailure(t);
			}
			public void onSuccess(Object result) {
				Build[] builds = (Build[]) result;
				lastBuilds=builds;
				asyncCallback.onSuccess(builds);
			}
		});

//		calService.getBuilds(new AsyncCallback() {
//			public void onFailure(Throwable caught) {
//				acceptor.failed(caught);
//			}
//
//			public void onSuccess(Object result) {
//				Build[] builds = (Build[]) result;
//				ArrayList list=new ArrayList(); 
//				for (int i = 0; i < builds.length; i++) {
//					Build build = builds[i];
//					if(filter==null || filter.accept(build))
//						list.add(build);
//				}
////				lastStartRow = startRow;
////				lastMaxRows = maxRows;
////				lastPeople = people;
//				builds=new Build[list.size()];
//				int i=0;
//				for (Iterator iter = list.iterator(); iter.hasNext();) {
//					Build build = (Build) iter.next();
//					builds[i++]=build;
//				}
//				lastBuilds=builds;
//				pushResults(acceptor, builds);
//			}
//		});
	}
	
//	public void refresh()
//	{
//		calService.refresh(new AsyncCallback () {
//			public void onFailure(Throwable caught) {
//				
//			}
//			public void onSuccess(Object result) {
//				
//			}
//		});
//	}

//	private void pushResults(RowDataAcceptor acceptor, //int startRow, int maxRows,
//			Build[] builds) {
//		String[][] rows = new String[builds.length][];
//		for (int i = 0, n = rows.length; i < n; i++) {
//			Build build = builds[i];
//			rows[i] = new String[4];
//			rows[i][0] = build.getCvsTag();
//			rows[i][1] = build.getVersionNumber();
//			rows[i][2] = build.getBuildNumber();
//			rows[i][3] = build.getReleaseNumber();
//		}
//		acceptor.accept(/*startRow, maxRows,*/ rows);
//	}

	public void reload(AsyncCallback asyncCallback)
	{
		lastBuilds=null;
//		calService.refresh(new AsyncCallback () {
//			public void onFailure(Throwable t)
//			{
//				
//			}
//			public void onSuccess(Object o)
//			{
//				// nop: check that next step is fine
//			}
//		});
		calService.refresh(asyncCallback);
	}

	public void delete(Build build, AsyncCallback asyncCallback)
	{
		calService.delete(build, asyncCallback);
	}
}
